//
//  FDSuggestingController.m
//  FamousDoctorOnline
//
//  Created by 丁鼎 on 16/7/6.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDSuggestingController.h"
#import "SVProgressHUD.h"

@interface FDSuggestingController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>

@property (nonatomic,strong)UIView *suggetView;
@property (nonatomic,strong)UITextView *suggestField;
@property (nonatomic,strong)UILabel *suggestLabel;
@property (nonatomic,strong)UIButton *suggetButton;
@property (nonatomic,strong)UITableView *suggestTableView;
@property (nonatomic,strong)UIView *midView;
@property (nonatomic,assign)NSInteger titlecount;
@property (nonatomic,strong)UILabel *placeHold;
@property (nonatomic,strong)NSArray *dataArray;



@end

@implementation FDSuggestingController

//懒加载数据
-(NSArray *)dataArray
{
    if(!_dataArray)
    {
        _dataArray = @[@{@"img":@"dianhua",@"text":@"官服电话:400-636-6113"},@{@"img":@"qq",@"text":@"用户交流群: 464768569"}];
    }
    return _dataArray;
}

-(UILabel *)placeHold
{
    if(!_placeHold )
    {
        _placeHold  = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 100, 30)];
        
        _placeHold.text = @"请输入反馈信息";
        _placeHold.font = [UIFont systemFontOfSize:12];
        _placeHold.textColor = [UIColor lightGrayColor];
    }
    return _placeHold;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
}

-(void)setupUI
{
    [self setupSuggetView];
    
    [self setupMidView];
    
    [self  setupSuggetTableView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
}

-(void)setupSuggetView
{
    //设置意见反馈输入模块
    UIView *suggetView = [[UIView alloc]init];
    [self.view addSubview:suggetView];
    self.suggetView = suggetView;
    [suggetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(74);
        make.left.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(200);
    }];

    suggetView.backgroundColor = [UIColor whiteColor];
    
    
    UITextView *suggestField = [[UITextView alloc]init];
    self.suggestField = suggestField;
    self.automaticallyAdjustsScrollViewInsets = NO;
    suggestField.backgroundColor = FDColor(242, 241, 246);
    suggestField.delegate = self;
    
    [self.suggetView addSubview:suggestField];
    [self.suggestField addSubview:self.placeHold];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(suggestFieldchaged) name:UITextViewTextDidChangeNotification object:nil];
    [suggestField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.suggetView.mas_left);
        make.top.equalTo(self.suggetView.mas_top);
        make.bottom.mas_equalTo(-50);
        make.width.equalTo(self.suggetView.mas_width);
    }];
    
    
    
//    一个label实时监听上面的输入框的变化,用代理方法
   
//    一个按钮,我也不知道有没有用
    UIButton *suggestButton = [[UIButton alloc]init];
    self.suggetButton = suggestButton;
    [suggestButton addTarget:self action:@selector(clickSuggest) forControlEvents:UIControlEventTouchUpInside];
    [suggetView addSubview:suggestButton];
    [suggestButton  setImage:[UIImage imageNamed:@"dijiaofankui"] forState:UIControlStateNormal];
    
    [suggestButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.equalTo(suggetView.mas_right);
        make.top.equalTo(self.suggestField.mas_bottom).offset(10);
        make.width.mas_equalTo(69);
        make.height.mas_equalTo(31);
        
        
    }];
    
   
    
    
    UILabel *suggestLabel = [[UILabel alloc]init];
    self.suggestLabel = suggestLabel;
   
    [suggetView addSubview:suggestLabel];
    suggestLabel.text = [NSString stringWithFormat:@"您还可以输入的字数为: %d",200];
    
    [suggestLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(suggestField.mas_bottom).offset(10);
        make.bottom.mas_equalTo(-10);
        make.right.equalTo(suggestButton.mas_left).offset(-20);
        
        
    }];
    [suggestLabel sizeToFit];
    
    
  
}

-(void)setupMidView
{
    UIView *midView = [[UIView alloc]init];
    self.midView = midView;
    midView.backgroundColor = FDColor(242, 241, 246);
    [self.view addSubview:midView];
    
    [midView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view.mas_width);
        make.top.equalTo(self.suggetView.mas_bottom);
        make.left.mas_equalTo(0);
        make.height.mas_equalTo(100);
        
    }];
    UILabel *tmpLabel = [[UILabel alloc]init];
    tmpLabel.text = @"欢迎联系我们";
    tmpLabel.font = [UIFont systemFontOfSize:20];
    tmpLabel.textColor = [UIColor blackColor];
    [tmpLabel sizeToFit];
    [midView addSubview:tmpLabel];
    
    [tmpLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.midView.mas_centerY);
        make.left.equalTo(midView.mas_left).offset(20);
        
    }];
    
    
}

-(void)setupSuggetTableView
{
    UITableView *suggestTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.suggestTableView = suggestTableView;
    self.suggestTableView.delegate = self;
    self.suggestTableView.dataSource = self;
    
    self.suggestTableView.tableFooterView = [[UIView alloc]init];
    
    [self.view addSubview:suggestTableView];
    
    [suggestTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view.mas_width);
        make.top.equalTo(self.midView.mas_bottom);
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
}

#pragma mark -tableview的数据源方法
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.imageView.image = [UIImage imageNamed:self.dataArray[indexPath.row][@"img"]];
    cell.textLabel.text = self.dataArray[indexPath.row][@"text"];
    return cell;
    
}



-(void)suggestFieldchaged
{
    self.suggestLabel.text = [NSString stringWithFormat:@"您还可以输入的字数为: %lu",200-self.suggestField.text.length];
    if(self.suggestField.text.length == 0)
    {
        [self.suggestField addSubview:self.placeHold];
    }else{
        [self.placeHold removeFromSuperview];
    }
 
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
    if(range.location >= 200)
    {
        return NO;
    }
    else
    {
        return YES;
    }
    
}

-(void)clickSuggest
{
//    self.suggestField.text = @"";
    [SVProgressHUD showSuccessWithStatus:@"反馈提交成功"];
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    
}

@end
